#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author   : Moty
# @Time     : 2024/10/18 0:10
# @File     : kc16_15.py
# @Project  : jt808


def parse_15_Charging_Handshake(body):
    # 解析0x15 充电握手
    data = {
        "交易流水号": f"{body[0:32]}({body[0:32]})",
        "桩编号": f"{body[32:46]}({body[32:46]})",
        "枪号": f"{int(body[46:48], 16)}({body[46:48]})",
        "BMS 通信协议版本号": f"V{int(body[50:52], 16)}.{int(body[52:54], 16)}({body[48:54]})",
        "BMS 电池类型": f"{battery_type(body[54:56])}({body[54:56]})",
        "BMS 整车动力蓄电池系统额定容量": f"{int(body[56:60], 16)/10}Ah/位({body[56:60]})",
        "BMS 整车动力蓄电池系统额定总电压": f"{int(body[60:64], 16)/10}V/位({body[60:64]})",
        "BMS 电池生产厂商名称": f"{bytes.fromhex(body[64:72]).decode('ascii')}({body[64:72]})",
        "BMS 电池组序号": body[72:80],
        "BMS 电池组生产日期年": f"{int(body[80:82], 16) + 1985}年({body[80:82]})",
        "BMS 电池组生产日期月": f"{int(body[82:84], 16)}月({body[82:84]})",
        "BMS 电池组生产日期日": f"{int(body[84:86], 16)}日({body[86:88]})",
        "BMS 电池组充电次数": f"{int(body[86:92], 16)}次/位({body[86:92]})",
        "BMS 电池组产权标识": body[92:94],
        "预留位": f"{body[94:96]}({body[94:96]})",
        "BMS 车辆识别码": f"{body[96:130]}({body[96:130]})",
        "BMS 软件版本号": f"{int.from_bytes(bytes.fromhex(body[136:140]), byteorder='little')}年{int(body[140:142], 16)}月{int(body[142:144], 16)}日 流水号:{int(body[144:146], 16)}({body[130:146]})",
    }
    formatted_data = ""
    for key, value in data.items():
        formatted_data += f"{key}: {value}\n"
    return formatted_data


def battery_type(result):
    return "铅酸电池" if result == "01" else "氢电池" if result == "02" else "磷酸铁锂电池" if result == "03" \
        else "锰酸锂电池" if result == "04" else "钴酸锂电池" if result == "05" else "三元材料电池" if result == "06" \
        else "聚合物锂离子电池" if result == "07" else "其他" if result == "FF" else "未知"


__all__ = ['parse_15_Charging_Handshake']